"use client";
import { DictData } from "@/lib/api/system/dict.d";
import {
  addDictDataApi,
  getDictDataApi,
  updateDictDataApi,
} from "@/lib/api/system/dict";
import {
  PageContainer,
  ProForm,
  ProFormDigit,
  ProFormText,
  ProFormTextArea,
} from "@ant-design/pro-components";
import { useRequest } from "ahooks";
import { Button, Card, Col, message, Row, Space } from "antd";
import { useParams, useRouter, useSearchParams } from "next/navigation";
import { useState } from "react";
import { RollbackOutlined } from "@ant-design/icons";

export default function Page() {
  const { type } = useParams();
  const router = useRouter();
  const [dictCode, setDictCode] = useState<string>("");
  const searchParams = useSearchParams();
  const dictType = searchParams.get("dictType");

  const title = type === "add" ? `新增字典数据` : `修改字典数据`;

  const { run: addDictData, loading: addDictDataLoading } = useRequest(
    addDictDataApi,
    {
      manual: true,
      onSuccess: () => {
        message.success("新增成功");
        router.back();
      },
      onError: (error: any) => {
        message.error(error.msg);
      },
    },
  );

  const { run: updateDictData, loading: updateDictDataLoading } = useRequest(
    (params) => updateDictDataApi({ ...params, dictCode }),
    {
      manual: true,
      onSuccess: () => {
        message.success("修改成功");
        router.back();
      },
      onError: (error: any) => {
        message.error(error.msg);
      },
    },
  );

  const extraProps =
    type !== "add"
      ? {
          request: async () => {
            const res = await getDictDataApi(type as string);
            setDictCode(res.data.dictCode);
            return res.data;
          },
        }
      : {
          initialValues: {
            dictType,
          },
        };

  return (
    <PageContainer
      title={title}
      extra={
        <Button icon={<RollbackOutlined />} onClick={() => router.back()}>
          返回
        </Button>
      }
    >
      <Card>
        <ProForm
          layout="horizontal"
          submitter={{
            render: (_, dom) => {
              return (
                <Row>
                  <Col span={14} offset={4}>
                    <Space>{dom}</Space>
                  </Col>
                </Row>
              );
            },
            searchConfig: { submitText: "保存" },
          }}
          className="max-w-[800px] mx-auto my-10"
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 14 }}
          loading={addDictDataLoading || updateDictDataLoading}
          onFinish={async (values: DictData) => {
            if (type === "add") {
              await addDictData(values);
            } else {
              await updateDictData(values);
            }
          }}
          {...extraProps}
        >
          <ProFormText name="dictType" label="字典类型" disabled />
          <ProFormText
            name="dictLabel"
            label="数据标签"
            required
            rules={[{ required: true, message: "请输入字典名称" }]}
          />
          <ProFormText
            name="dictValue"
            label="数据键值"
            required
            rules={[{ required: true, message: "请输入字典类型" }]}
          />
          <ProFormDigit
            min={0}
            max={100}
            fieldProps={{
              precision: 0,
            }}
            name="dictSort"
            label="排序"
            required
            rules={[{ required: true, message: "请输入排序" }]}
          />
          <ProFormTextArea name="remark" label="备注" />
        </ProForm>
      </Card>
    </PageContainer>
  );
}
